package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.XcxSubscribeKeywordBo;
import org.dromara.system.domain.vo.XcxSubscribeKeywordVo;
import org.dromara.system.domain.XcxSubscribeKeyword;
import org.dromara.system.mapper.XcxSubscribeKeywordMapper;
import org.dromara.system.service.IXcxSubscribeKeywordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订阅关键字Service业务层处理
 *
 * @author Lion Li
 * @date 2024-07-01
 */
@RequiredArgsConstructor
@Service
public class XcxSubscribeKeywordServiceImpl implements IXcxSubscribeKeywordService {

    private final XcxSubscribeKeywordMapper baseMapper;

    /**
     * 查询订阅关键字
     */
    @Override
    public XcxSubscribeKeywordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订阅关键字列表
     */
    @Override
    public TableDataInfo<XcxSubscribeKeywordVo> queryPageList(XcxSubscribeKeywordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<XcxSubscribeKeyword> lqw = buildQueryWrapper(bo);
        Page<XcxSubscribeKeywordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订阅关键字列表
     */
    @Override
    public List<XcxSubscribeKeywordVo> queryList(XcxSubscribeKeywordBo bo) {
        LambdaQueryWrapper<XcxSubscribeKeyword> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XcxSubscribeKeyword> buildQueryWrapper(XcxSubscribeKeywordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<XcxSubscribeKeyword> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, XcxSubscribeKeyword::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getKeyword()), XcxSubscribeKeyword::getKeyword, bo.getKeyword());
        return lqw;
    }

    /**
     * 新增订阅关键字
     */
    @Override
    public Boolean insertByBo(XcxSubscribeKeywordBo bo) {
        XcxSubscribeKeyword add = MapstructUtils.convert(bo, XcxSubscribeKeyword.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订阅关键字
     */
    @Override
    public Boolean updateByBo(XcxSubscribeKeywordBo bo) {
        XcxSubscribeKeyword update = MapstructUtils.convert(bo, XcxSubscribeKeyword.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XcxSubscribeKeyword entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订阅关键字
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<XcxSubscribeKeywordVo> queryListKeyword(XcxSubscribeKeywordBo bo) {
        LambdaQueryWrapper<XcxSubscribeKeyword> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoListKeyword(lqw);
    }

    @Override
    public List<Long> queryKeywordUserId(String keyword, String projectUnit) {
        return baseMapper.selectVoListUserId(keyword,projectUnit);
    }
}
